// 健康检查路由
const express = require("express");
const wechatConfig = require("../config/wechat");

const router = express.Router();

// 健康检查接口
router.get("/health", (req, res) => {
  const config = wechatConfig.getConfig();

  res.json({
    status: "ok",
    timestamp: new Date().toISOString(),
    version: process.env.npm_package_version || "1.0.0",
    node_env: process.env.NODE_ENV || "development",
    wechat_config: {
      appid:
        config.appid &&
        config.appid !== "YOUR_APPID_HERE" &&
        config.appid !== "wx1252cf1f26c7faee"
          ? "configured"
          : "missing",
      secret:
        config.secret &&
        config.secret !== "YOUR_APPSECRET_HERE" &&
        config.secret !== "127a12fe490f3d9852f96e2cecea1888"
          ? "configured"
          : "missing",
    },
    uptime: process.uptime(),
    memory: {
      used: Math.round(process.memoryUsage().heapUsed / 1024 / 1024) + "MB",
      total: Math.round(process.memoryUsage().heapTotal / 1024 / 1024) + "MB",
    },
  });
});

// 服务信息接口
router.get("/info", (req, res) => {
  res.json({
    name: "微信授权服务",
    description: "微信扫一扫授权获取用户openid的完整解决方案",
    version: process.env.npm_package_version || "1.0.0",
    endpoints: {
      auth: {
        openid: "/api/auth/openid",
        userinfo: "/api/auth/userinfo",
        check_token: "/api/auth/check-token",
      },
      health: "/api/health",
      pages: {
        home: "/",
        auth: "/auth",
        demo: "/demo",
      },
    },
    environment: process.env.NODE_ENV || "development",
  });
});

module.exports = router;
